package br.uvv.Web.ManagedBean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import br.uvv.Model.ClientePortal;
import br.uvv.Model.Usuario;
import br.uvv.Service.ClientePortalService;
import br.uvv.Util.InitUtil;
import br.uvv.Util.MsgUtil;
import br.uvv.Util.RNException;
import br.uvv.Util.Util;

public class ClientePortalBean implements Serializable {

	private static final long serialVersionUID = -281512421942607770L;

	private ClientePortalService service;
	
	private ClientePortal clientePortalInstance;
	private ClientePortal clientePortalInstanceTemp;
	private LazyDataModel<ClientePortal> clientePortalList;
	
	private String varBuscaClientePortal;
	
	private void init(){
		service = new ClientePortalService();
		clientePortalInstance = new ClientePortal();
		clientePortalInstanceTemp = new ClientePortal();
		clientePortalList = InitUtil.inicializaClientePortalList();
		varBuscaClientePortal = "";
	}
	
	public String clientePortal(){
		init();
		return "/views/clientePortal/show.xhtml";
	}
	
	public String goCreate(){
		clientePortalInstance = new ClientePortal();
		clientePortalInstance.setResponsavel(new Usuario());
		return "/views/clientePortal/create.xhtml";
	}
	
	public String goEdit(){
		return "/views/clientePortal/edit.xhtml";
	}
	
	public String goCancel(){
		return "/views/clientePortal/show.xhtml";
	}
	
	public String salvar(){
		try {
			service.salvarClientePortal(clientePortalInstance);
			MsgUtil.info("Sucess");
			return "/views/clientePortal/show.xhtml";
		} catch (RNException e) {
			MsgUtil.error("Error");
			e.printStackTrace();
			return null;
		}
	}
	
	public void excluir(){
		try {
			service.excluir(clientePortalInstance);
			MsgUtil.info("Sucess");
		} catch (RNException e) {
			MsgUtil.error("Error");
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("serial")
	public void filtrarClientePortal(){
		clientePortalList = new LazyDataModel<ClientePortal>() {
			@Override
			public List<ClientePortal> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
				Util.setDataTableRows(pageSize);
				try {
					return service.listarClientePortal(first, pageSize, sortField, sortOrder, varBuscaClientePortal);
				} catch (RNException e) {
					e.printStackTrace();
					return new ArrayList<ClientePortal>();
				}
			}
			@Override  
	        public ClientePortal getRowData(String rowKey)  
	        {  
	        	try {
		            return service.selecionarClientePortal(Long.parseLong(rowKey));
	        	} catch (RNException e) {
	        		e.printStackTrace();
	        		return new ClientePortal();
	        	} catch (NumberFormatException e){
	        		return new ClientePortal();
	        	}
	        }  
	        @Override  
	        public String getRowKey(ClientePortal objeto)  
	        {  
	        	if (objeto!=null)
	        		return ""+objeto.getId();
	        	return "";
	        }  
		};
		try {
			clientePortalList.setRowCount(service.countClientePortal(varBuscaClientePortal));
		} catch (RNException e) {
			e.printStackTrace();
		}
		clientePortalList.setPageSize(5);
	}
	
	public void selecionarClientePortal(){
		try {
			clientePortalInstance = service.selecionarClientePortalComUsuarios(clientePortalInstanceTemp.getId());
		} catch (RNException e) {
			e.printStackTrace();
		}
		cancelarSelecionarClientePortal();
	}
	
	public void cancelarSelecionarClientePortal(){
		clientePortalInstanceTemp = null;
		varBuscaClientePortal = null;
		clientePortalList = InitUtil.inicializaClientePortalList();
	}
	
	public String goHome(){
		service = null;
		clientePortalInstance = null;
		clientePortalInstanceTemp = null;
		clientePortalList = null;
		varBuscaClientePortal = null;
		return "goHome";
	}

	public ClientePortal getClientePortalInstance() {
		return clientePortalInstance;
	}

	public void setClientePortalInstance(ClientePortal clientePortalInstance) {
		this.clientePortalInstance = clientePortalInstance;
	}

	public ClientePortal getClientePortalInstanceTemp() {
		return clientePortalInstanceTemp;
	}

	public void setClientePortalInstanceTemp(ClientePortal clientePortalInstanceTemp) {
		this.clientePortalInstanceTemp = clientePortalInstanceTemp;
	}

	public LazyDataModel<ClientePortal> getClientePortalList() {
		return clientePortalList;
	}

	public void setClientePortalList(LazyDataModel<ClientePortal> clientePortalList) {
		this.clientePortalList = clientePortalList;
	}

	public String getVarBuscaClientePortal() {
		return varBuscaClientePortal;
	}

	public void setVarBuscaClientePortal(String varBuscaClientePortal) {
		this.varBuscaClientePortal = varBuscaClientePortal;
	}
}